Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

An Application Example that Consumes Messages from a Remote J2EE Server

This section and the following section both explain how to write, compile, package, deploy, and run a pair of J2EE applications that use the JMS API and run on two J2EE servers. A common practice is to deploy different components of an enterprise application on different systems within a company, and these examples illustrate on a small scale how to do this for an application that uses the JMS API.

However, the two examples work in slightly different ways. In this first example, the deployment information for a message-driven bean specifies the remote server from which it will consume messages. In the next example, the same bean is deployed on two different servers, so it is the client application that specifies the servers (one local, one remote) to which it is sending messages.

This first example divides the example in A Simple J2EE Application that Uses the JMS API into two applications, one containing the application client and the other containing the message-driven bean.

The section covers the following topics:

You will find the source files for this section in <INSTALL>/j2eetutorial14/examples/jms/consumeremote/. Path names in this section are relative to this directory.

Overview of the Applications

Except for the fact that it is packaged as two separate applications, this example is almost identical to the one in A Simple J2EE Application that Uses the JMS API:

The basic steps of the applications are as follows.

  1. The administrator starts two J2EE servers, one on each system.
  2. On one server, the administrator deploys the client application, which sends three messages to a queue.
  3. On the other server, the administrator deploys the message-driven bean application, which specifies the URL of the server where the client is deployed.
  4. The message-driven bean consumes the messages.

Figure 21-4 illustrates the structure of this application. You can see that it is almost identical to Figure 21-1 except that there are two J2EE servers. The queue is not associated with either server; it must exist on both servers.

A J2EE Application that Consumes Messages from a Remote Server

Figure 21-4 A J2EE Application that Consumes Messages from a Remote Server

Writing the Application Components

Writing the components of the applications involves

The application client, earthclient/src/SimpleClient.java, is identical to the one in Coding the Application Client: SimpleClient.java.

Similarly, the message-driven bean, marsmdb/src/MessageBean.java, is identical to the one in Coding the Message-Driven Bean: MessageBean.java.

The only major difference is that the client and bean are packaged in two separate applications.

Creating and Packaging the Applications

This example uses the connection factory named jms/QueueConnectionFactory and the queue named jms/Queue, both of which you created in Writing Simple JMS Client Applications.

We'll suppose, as we did in Running JMS Client Programs on Multiple Systems, that the two servers are named earth and mars.

Creating and packaging this application involve several steps:

Starting the J2EE Application Server and Compiling the Source Files

Which system you use to package and deploy the application and which system you use to run the client depends on your network configuration. If both systems are visible to each other, it does not matter which system you use for which tasks. If only one system is visible to the other, do the packaging and deployment on the system from which the other system is visible, and run the client on the other system. These instructions assume that earth is visible from mars, but mars is not visible from earth.

  1. Start the J2EE Application Server on earth, if it is not already running.
  2. Start the J2EE Application Server on mars, if it is not already running.
  3. Also on mars, go to the directory earthclient. Compile the source files:
  4. asant build

  5. Also on mars, go to the directory marsmdb. Compile the source files:
  6. asant build

Starting the J2EE Deploytool and Creating the Applications

On mars, do the following to create the application for the client:

  1. Start the deploytool.
  2. Choose FileRight ArrowNewRight ArrowApplication EAR.
  3. Click Browse next to the Application File Name field and use the file chooser to locate the directory earthclient.
  4. In the File Name field, type EarthClientApp.
  5. Click New Application.
  6. Click OK.

On mars, do the following to create the application for the message-driven bean:

  1. Choose FileRight ArrowNewRight ArrowApplication EAR.
  2. Click Browse next to the Application File Name field and use the file chooser to locate the directory marsmdb.
  3. In the File Name field, type MarsMDBApp.
  4. Click New Application.
  5. Click OK.

Packaging the Application Client

Perform the following steps to package the application client:

  1. Choose FileRight ArrowNewRight ArrowApplication Client JAR to start the Application Client Wizard.
  2. In the JAR File Contents screen:
    1. Verify that Create New AppClient Module in Application is selected and that the application is EarthClientApp.
    2. In the AppClient Display Name field, type SimpleClient.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the earthclient/build/ directory. Select SimpleClient.class from the Available Files tree area and click Add, then OK.
  3. In the General screen, select SimpleClient in the Main Class combo box.
  4. In the Message Destination References screen, click Add. In the dialog box:
    1. Type jms/QueueName in the Coded Name field.
    2. Choose javax.jms.Queue from the Destination Type menu.
    3. Choose Produces from the Usage menu.
    4. Type PhysicalQueue in the Destination Name field.
  5. In the Resource References screen:
    1. Click Add.
    2. Type jms/MyConnectionFactory in the Coded Name field.
    3. Choose javax.jms.ConnectionFactory from the Type menu.
    4. Type jms/QueueConnectionFactory in the JNDI name field and j2ee in both the User Name and Password fields.
  6. Click Finish.
  7. Click the Message Destinations tab. In the inspector pane:
    1. Click Add.
    2. Type PhysicalQueue in the Destination Name field. When you press Enter, this name appears in the Display Name field, and SimpleClient appears in the Producers area.
    3. Type jms/Queue in the JNDI Name field.

Packaging the Message-Driven Bean

Perform the following steps to package the message-driven bean:

  1. Choose FileRight ArrowNewRight ArrowEnterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
  2. In the EJB JAR screen:
    1. Select Create New JAR Module in Application and verify that the application is MarsMDBApp.
    2. In the JAR Display Name field, type MDBJAR.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the marsmdb/build/ directory. Select MessageBean.class from the Available Files tree area and click Add, then OK.
  3. In the General screen:
    1. Choose the Message-Driven radio button.
    2. From the Enterprise Bean Class menu, choose MessageBean.
    3. In the Enterprise Bean Display Name field, accept the default name, MessageBean.
  4. In the Configuration Options screen, check the Transaction Management box.
  5. In the Message-Driven Bean Settings screen:
    1. Click Add in the Activation Configuration Properties area.
    2. Type destination in the Property Name field and PhysicalQueue in the Value field.
    3. Click Add again.
    4. Type destinationType in the Property Name field and javax.jms.Queue in the Value field.
    5. Click Add again.
    6. Type addressList in the Property Name field and the name of the remote system in the Value field. This means that the bean will consume messages from the remote system.
    7. Choose javax.jms.Queue from the Destination Type menu.
    8. Type PhysicalQueue in the Destination Name field.
    9. Click Deployment Settings. In the dialog box, type jmsra in the Resource Adapter field and click OK.
  6. In the Transaction Management screen, choose Container-Managed.
  7. Click Finish.
  8. Click the Message Destinations tab. In the inspector pane:
    1. Click Add.
    2. Type PhysicalQueue in the Destination Name field. When you press Enter, this name appears in the Display Name field, and MessageBean appears in the Consumers area.
    3. Type jms/Queue in the JNDI Name field.

Verifying and Updating the JNDI Names

In the J2EE Application Server, the JNDI name for a message-driven bean is the destination it receives messages from, not the bean name. You need to update the JNDI name for the MarsMDBApp application and verify it for the EarthClientApp application.

  1. Select the MarsMDBApp application and click the JNDI Names tab.
  2. Type jms/Queue in the JNDI Name field for MessageBean.

The JNDI name for the application should appear as shown in Table 21-8. Only the Application Pane has any content.

Table 21-8 Application Pane for MarsMDBApp
Component Type
Component
JNDI Name
EJB
MessageBean
jms/Queue

Select the EarthClientApp application and click the JNDI Names tab.

The JNDI name for the application should appear as shown in Table 21-9. Only the References Pane has any content.

Table 21-9 References Pane for EarthClientApp
Ref. Type
Referenced By
Reference Name
JNDI Name
Resource
SimpleClient
jms/MyConnectionFactory
jms/QueueConnectionFactory

Deploying the Applications

Before you can deploy the applications, you must add the remote server. Perform the following steps:

  1. Choose FileRight ArrowAdd Server.
  2. Type the name of the remote system in the Server Name field and click OK.
  3. The server appears in the tree under Servers. Select it.
  4. In the dialog box that appears, type the administrative user name and password for the server in the Connection Settings area and click OK.

To deploy the EarthClientApp application, perform the following steps:

  1. Choose FileRight ArrowSave to save the application.
  2. Choose ToolsRight ArrowDeploy.
  3. In the dialog box, select the URI for the remote system:
  4. deployer:Sun:S1AS::system-name:4848

  5. Type your administrative user name and password (if they are not already filled in) and click OK.
  6. In the Distribute Module dialog box, click Close when the process completes.

To deploy the MarsMDBApp application, perform the following steps:

  1. Choose FileRight ArrowSave to save the application.
  2. Choose ToolsRight ArrowDeploy.
  3. In the dialog box, choose the URI for localhost from the menu.
  4. Type your administrative user name and password (if they are not already filled in) and click OK.
  5. In the Distribute Module dialog box, click Close when the process completes.

Saving the Client JAR and Running the Application Client

Perform the following steps:

  1. In the deploytool, select the server node.
  2. Select EarthClientApp from the list of deployed objects, then click Client Jar.
  3. Choose Browse to navigate to the directory on the remote system from which you will run the client. When you reach the directory, click Select, then click OK. Click OK in the information dialog. You will find a file named EarthClientAppClient.jar in the specified directory.

To run the client,

  1. Go to the directory on the remote system where you created the client JAR file.
  2. Use the following command:
  3. appclient -client EarthClientAppClient.jar

On earth, the output of the appclient command looks something like this:

INFO: Binding name:`java:comp/env/jms/QueueName`
Sending message: This is message 1
Sending message: This is message 2
Sending message: This is message 3 

On mars, the output in the server log looks like this:

In MessageBean.MessageBean()
In MessageBean.setMessageDrivenContext()
In MessageBean.ejbCreate()
MESSAGE BEAN: Message received: This is message 1
MESSAGE BEAN: Message received: This is message 2
MESSAGE BEAN: Message received: This is message 3 

Undeploying and Removing the Applications

To undeploy and remove the applications, perform the following steps:

  1. Select localhost.
  2. Select MarsMDBApp in the server pane and click Undeploy.
  3. Select the application in the left-hand pane and choose FileRight ArrowClose.
  4. Repeat steps 1-3 for EarthClientApp on the remote system.
  5. Remove the build directory:
  6. asant clean

  7. If you wish, stop the server:
  8. asadmin stop-domain

If you wish, you can manually delete the EAR and client JAR files.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.